package com.mask.acl.controller;

import com.alibaba.fastjson.JSONObject;
import com.mask.acl.service.IndexService;
import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.util.RollResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/acl/index")
@Api(tags = "首页管理")
public class IndexController {

    @Autowired
    private IndexService indexService;

    /**
     * 根据token获取用户信息
     */
    @ApiOperation("根据token获取用户信息")
    @GetMapping("/info")
    public RollResponse info(){
        //获取当前登录用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        Map<String, Object> userInfo = indexService.getUserInfo(username);
        return RollResponse.result(ServiceEnums.ACL_GET_USER_INFO_BY_TOKEN_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.ACL_GET_USER_INFO_BY_TOKEN_SUCCESS.getMessage(),userInfo);
    }

    /**
     * 获取菜单
     * @return
     */
    @ApiOperation("获取菜单")
    @GetMapping("/menu")
    public RollResponse getMenu(){
        //获取当前登录用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        List<JSONObject> permissionList = indexService.getMenu(username);
        return RollResponse.result(ServiceEnums.ACL_GET_MENU_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.ACL_GET_MENU_SUCCESS.getMessage(),permissionList);
    }

    @ApiOperation("退出")
    @PostMapping("/logout")
    public RollResponse logout(){
        return RollResponse.result(ServiceEnums.ACL_LOGOUT_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.ACL_LOGOUT_SUCCESS.getMessage());
    }

}
